package com.cq.hd.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cq.hd.common.constant.Constant;
import com.cq.hd.common.constant.RedisKeyConstant;
import com.cq.hd.common.utils.BeanUtils;
import com.cq.hd.common.utils.LocalDateUtil;
import com.cq.hd.common.utils.RedisUtil;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.member.api.dto.WxSubscribeRecordSaveDto;
import com.cq.hd.member.api.vo.WxSubscribeRecordVo;
import com.cq.hd.member.po.TbWxSubscribeRecordPo;
import com.cq.hd.member.mapper.TbWxSubscribeRecordMapper;
import com.cq.hd.member.service.TbWxSubscribeRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户关注公众号记录表 服务实现类
 * </p>
 *
 * @author mYunYu
 * @since 2024-02-26
 */
@Service
public class TbWxSubscribeRecordServiceImpl extends ServiceImpl<TbWxSubscribeRecordMapper, TbWxSubscribeRecordPo> implements TbWxSubscribeRecordService {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Boolean saveWxSubscribeRecord(WxSubscribeRecordSaveDto wxSubscribeRecordSaveDto) {
        Long userId = wxSubscribeRecordSaveDto.getUserId();
        String openid = wxSubscribeRecordSaveDto.getOpenid();
        LocalDateTime subscribeTime = wxSubscribeRecordSaveDto.getSubscribeTime();

        if (userId == null || userId <= 0) {
            log.error("添加关注微信公众号失败，userId为空");
            return false;
        }

        if (StringUtils.isBlank(openid)) {
            log.error("添加关注微信公众号失败，openid为空");
            return false;
        }

        TbWxSubscribeRecordPo tbWxSubscribeRecordPo = new TbWxSubscribeRecordPo();
        tbWxSubscribeRecordPo.setUserId(userId);
        tbWxSubscribeRecordPo.setSubscribeTime(subscribeTime);
        tbWxSubscribeRecordPo.setOpenid(openid);
        tbWxSubscribeRecordPo.setCreateTime(LocalDateTime.now());
        tbWxSubscribeRecordPo.setUpdateTime(LocalDateTime.now());

        int count = baseMapper.insert(tbWxSubscribeRecordPo);
        if (count > 0) {
            redisUtil.hmSet(RedisKeyConstant.WX_PUB_SUBSCRIBE_KEY, String.valueOf(userId),
                    LocalDateUtil.toLocalDateTimeToString(subscribeTime, Constant.YYYY_MM_DD_HH_MM_SS));
        }

        return count > 0;
    }

    @Override
    public Boolean delByOpenId(String openid) {
        if (StringUtils.isBlank(openid)) {
            log.error("删除关注微信公众号记录失败，openid为空");
            return false;
        }

        List<TbWxSubscribeRecordPo> wxSubscribeRecordPos = baseMapper.selectList(new QueryWrapper<TbWxSubscribeRecordPo>().eq("openid", openid));
        if (!CollectionUtils.isEmpty(wxSubscribeRecordPos)) {
            baseMapper.delete(new QueryWrapper<TbWxSubscribeRecordPo>().eq("openid", openid));

            for (TbWxSubscribeRecordPo wxSubscribeRecordPo : wxSubscribeRecordPos) {
                Long userId = wxSubscribeRecordPo.getUserId();
                if (userId != null && userId > 0) {
                    redisUtil.hDel(RedisKeyConstant.WX_PUB_SUBSCRIBE_KEY, String.valueOf(userId));
                }
            }
        }

        return true;
    }

    @Override
    public WxSubscribeRecordVo getByUserId(Long userId) {
        if (userId == null || userId <= 0) {
            log.error("获取关注微信公众号记录失败，userId为空");
            return null;
        }

        List<TbWxSubscribeRecordPo> tbWxSubscribeRecordPos = baseMapper.selectList(new QueryWrapper<TbWxSubscribeRecordPo>()
                .eq("del_state", 0).eq("user_id", userId).orderByDesc("create_time"));
        if (!CollectionUtils.isEmpty(tbWxSubscribeRecordPos)) {
            TbWxSubscribeRecordPo tbWxSubscribeRecordPo = tbWxSubscribeRecordPos.get(0);
            WxSubscribeRecordVo wxSubscribeRecordVo = new WxSubscribeRecordVo();
            BeanUtils.copyProperties(tbWxSubscribeRecordPo, wxSubscribeRecordVo);
            return wxSubscribeRecordVo;
        }

        return null;
    }
}
